<template>
  <div class="app-container">
    <el-card class="box-card">
      <div slot="header" class="clearfix">
        <el-row>
          <el-form :inline="true" :model="listQuery" ref="listQuery" class="demo-form-inline">
            <el-form-item label="">
              <el-date-picker
                v-model="listQuery.start"
                type="datetimerange"
                range-separator="至"
                start-placeholder="开始日期"
                end-placeholder="结束日期">
              </el-date-picker>
            </el-form-item>
            <el-form-item label="">
              <el-input type="text" v-model="listQuery.keyword" placeholder="请输入操作内容"></el-input>
            </el-form-item>
            <el-form-item>
              <el-button type="primary" @click="searchSubmit">查询</el-button>
              <el-button type="primary" @click="resetSubmit">重置</el-button>
            </el-form-item>
          </el-form>
        </el-row>
      </div>
      <div class="item" v-loading="listLoading">
        <el-table ref="dataList" :data="menuList" style="width:100%;margin-bottom: 20px;" row-key="id" border
                  :tree-props="{children: 'children', hasChildren: 'hasChildren'}">
          <!-- <el-table-column type="selection"></el-table-column> -->
          <el-table-column prop="log_id" label="序号"></el-table-column>
          <el-table-column prop="admin.admin_name" label="管理员"></el-table-column>
          <el-table-column prop="client_ip" label="IP"></el-table-column>
          <el-table-column prop="date" label="操作时间"></el-table-column>
          <el-table-column prop="uri" label="uri"></el-table-column>
          <el-table-column prop="method" label="方法">
            <template slot-scope="{row}">
              <el-tag style="color: #fff" :color="getColor(row.method)" v-text="row.method || '未知'"></el-tag>
            </template>
          </el-table-column>
          <el-table-column prop="content" label="操作内容"></el-table-column>

          <!-- <el-table-column label="操作" width="180px">
              <template slot-scope="scope">
              <el-button type="primary" size="mini" icon="el-icon-edit" @click="handleUpdate(scope.row)" plain>编辑</el-button>
              <el-button type="danger" size="mini" icon="el-icon-delete" @click="handleDelete(scope.row)" plain>删除</el-button>
              </template>
          </el-table-column> -->
        </el-table>
        <!-- 分页 -->
        <cus-pagination
          :total="total"
          :page.sync="listQuery.page"
          :limit.sync="listQuery.perPage"
          @pagination="getList"
        />
      </div>
    </el-card>
    <!-- 删除弹框 -->
    <el-dialog
      title="确定删除吗?"
      :visible.sync="deleteDialogVisible"
      width="435px"
    >
      <span style=""><em style="font-style:normal;color:red">删除后不可恢复</em>，你还要继续吗？</span>
      <span slot="footer" class="dialog-footer">
            <el-button @click="deleteDialogVisible = false">取 消</el-button>
            <el-button type="primary" @click="confirmDelete">确 定</el-button>
        </span>
    </el-dialog>
    <!-- 新增编辑弹框 -->
    <el-dialog v-dialogDrag :title="titleMap[dialogStatus]" :visible.sync="dialogFormVisible">
      <el-form :model="editRulesForm" :rules="editRulesFormRules" ref="editRulesForm" label-width="120px"
               class="demo-ruleForm">
        <el-form-item label="模块说明" prop="module_explain">
          <el-input v-model="editRulesForm.module_explain" placeholder="请输入模块名称"></el-input>
        </el-form-item>
        <el-form-item label="用户经验等级" prop="user_level">
          <el-input v-model="editRulesForm.user_level" placeholder="请输入此模块需要的用户经验等级"></el-input>
        </el-form-item>
        <el-form-item label="黑名单类别" prop="blackList_type">
          <el-input v-model="editRulesForm.blackList_type" placeholder="请输入所属黑名单类别"></el-input>
        </el-form-item>
        <el-form-item label="状态" prop="status">
          <el-radio v-model="editRulesForm.status" label='0'>开启</el-radio>
          <el-radio v-model="editRulesForm.status" label='1'>关闭</el-radio>
        </el-form-item>
        <el-form-item>
          <el-button @click="dialogFormVisible = false">返回</el-button>
          <el-button type="primary" @click="submitForm('editRulesForm')">提交</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>

<script>
import {
  listData
} from '@/api/config/logs'

export default {
  data() {
    return {
      // 黑名单类别
      blackList: [
        {value: 1, label: '1'},
        {value: 2, label: '4'},
        {value: 3, label: '6'},
        {value: 4, label: '3'},
        {value: 5, label: '5'}
      ],
      // 选中的黑名单类别
      checkBlackList: [],
      provinceList: [],
      cityList: [],
      // 表单规则
      editRulesFormRules: {
        module_explain: [{required: true, message: '请输入模块名称', trigger: 'blur'}],
        user_level: [{type: 'number', required: true, message: '请输入正确的用户经验等级', trigger: 'blur'}],
        blackList_type: [{required: true, message: '请输入正确的黑名单类别', trigger: 'blur'}],
      },
      // 受限类型
      limit_typeList: [
        {value: 1, label: '全部'},
        {value: 2, label: 'IP'},
        {value: 3, label: '归属地'},
      ],
      editRulesForm: {
        module_explain: undefined,
        user_level: undefined,
        blackList_type: undefined,
        status: '1',
      },
      dialogStatus: '',
      // dialog的头部信息
      titleMap: {
        update: '编辑',
        create: '添加'
      },
      dialogFormVisible: false,
      deleteDialogVisible: false,
      listLoading: false,
      menuList: [],
      limitList: [],
      listQuery: {
        page: 1,
        perPage: 10,
        keyword: undefined,
        start_time: null,
        start: null,
        end_time: null
      },
      total: 0,
      // 勾选内容
      multipleSelection: [],
      // 要删除的内容的ids
      dataIds: '',
    }
  },
  created() {
    this.getList()
  },
  methods: {
    getList() {
      this.listLoading = true;
      listData(this.listQuery).then((response) => {
        this.menuList = response.data.list;
        this.total = response.data.meta.total;
        this.listLoading = false
      })
    },
    // 点击查询
    searchSubmit() {
      this.listQuery.page = 1;
      if (this.listQuery.start) {
        this.listQuery.start_time = this.listQuery.start[0].getTime() / 1000;
        this.listQuery.end_time = this.listQuery.start[1].getTime() / 1000;
      }
      this.getList()
    },
    // 点击重置
    resetSubmit() {
      this.listQuery.page = 1;
      this.listQuery.perPage = 10
      this.listQuery.keyword = "";
      this.listQuery.start = null
      this.listQuery.start_time = null
      this.listQuery.end_time = null
      this.getList()
    },
    // 点击新增
    handleCreate() {
      this.resetForm('editRulesForm')
      this.dialogStatus = 'create'
      this.dialogFormVisible = true
    },
    // 点击编辑
    handleUpdate(row) {
      this.editRulesForm.module_explain = row.module_explain;
      this.editRulesForm.user_level = row.user_level;
      this.editRulesForm.blackList_type = row.blackList_type;
      this.editRulesForm.status = row.status;

      this.dialogStatus = 'update'
      this.dialogFormVisible = true
    },
    // 重置表单内容
    resetForm(formName) {
      this.$nextTick(() => {
        // this.$refs[formName].resetFields();
        if (this.$refs[formName] !== undefined) {
          this.$refs[formName].resetFields();
        }
        this.editRulesForm = {
          module_explain: undefined,
          user_level: undefined,
          blackList_type: undefined,
          status: '0',
        }
      })

    },
    // 获取省份
    getProvince() {

    },
    // 获取城市
    getCity() {

    },
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          // alert('submit!');
          this.dialogFormVisible = false
        } else {
          return false;
        }
      });
    },
    // 勾选
    handleSelectionChange(val) {
      this.multipleSelection = val;
    },
    // 点击单个删除
    handleDelete(row) {
      this.dataIds = {ids: `${row.id}`}
      this.deleteDialogVisible = true
    },
    // 点击批量删除
    handleDeleteAll() {
      if (this.multipleSelection.length === 0) {
        this.$message({
          message: '请先勾选要删除的内容',
          type: 'warning'
        });
        return;
      }
      this.deleteDialogVisible = true
    },
    // 确定删除
    confirmDelete() {
      if (this.multipleSelection.length !== 0) {
        let ids = '';
        for (let key in this.multipleSelection) {
          ids += `${this.multipleSelection[key].id},`
        }
        ids = ids.substring(0, ids.length - 1)
        this.dataIds = {ids: ids}
      }
      this.deleteDialogVisible = false;
      /* postDeleteRoom(this.dataIds).then((response) => {
          if (response.code === 200) {
              this.getList()
              this.deleteDialogVisible = false;
              this.$notify({
                  type:'success',
                  message: '操作成功',
                  position: 'bottom-right',
                  showClose: false,
                  duration: 3000
              });
          } else {
              this.$message({
                  message: response.message,
                  type: 'error'
              });
          }
      }) */
    },
    getColor(method) {
      /* 修复接口中 method 属性有肯能为 unll 的BUG */
      if (method) {
        switch (method.toLowerCase()) {
          case 'get':
            return '#68f141'
          case 'post':
            return '#4070ec'
          case 'put':
            return '#e5c500'
          case 'delete':
            return '#d1556f'
        }
      } else {
        return "#d1556f"
      }
    }
  }
}
</script>

<style scoped>
[v-cloak] {
  display: none;
}

.clearfix:before,
.clearfix:after {
  display: table;
  content: "";
}

.clearfix:after {
  clear: both
}

.box-card {
  width: 100%;
}

.demo-form-inline {
  float: right;
}

/deep/ .el-card__header {
  padding-bottom: 0;
}
</style>
